package com.songlin.testv2ex.model;

import android.os.Parcel;
import android.os.Parcelable;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by 松林 on 2015/10/4.
 */
public class Topic extends SuperModel implements Parcelable {
    public int id;
    public String title;
    public String url;
    public String content;
    public String contentRendered;
    public int replies;
    public Member member;
    public Node node;
    public long created;
    public long lastModified;
    public long lastTouched;

    public Topic(){

    }

    private Topic(Parcel in) {
        int[] ints = new int[2];
        in.readIntArray(ints);
        id = ints[0];
        replies = ints[1];
        String[] strings = new String[4];
        in.readStringArray(strings);
        title = strings[0];
        url = strings[1];
        content = strings[2];
        contentRendered = strings[3];
        long[] longs = new long[1];
        in.readLongArray(longs);
        created = longs[0];
        lastModified = longs[1];
        lastTouched = longs[2];
        member = (Member) in.readValue(Member.class.getClassLoader());
        node = (Node) in.readValue(Node.class.getClassLoader());
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int flags) {
        parcel.writeIntArray(new int[] {
                id, replies
        });
        parcel.writeStringArray(new String[]{
                title, url, content, contentRendered
        });
        parcel.writeValue(member);
        parcel.writeValue(node);
        parcel.writeLongArray(new long[] {
                created,    lastModified,   lastTouched
        });
    }

    @Override
    public void parse(JSONObject jsonObject) throws JSONException {
        id = jsonObject.getInt("id");
        title = jsonObject.getString("title");
        url = jsonObject.getString("url");
        content = jsonObject.getString("content");
        contentRendered = jsonObject.getString("content_rendered");
        replies = jsonObject.getInt("replies");
        member = new Member();
        member.parse(jsonObject.getJSONObject("member"));
        node = new Node();
        node.parse(jsonObject.getJSONObject("node"));
        created = jsonObject.getLong("created");
        lastModified = jsonObject.getLong("lastModified");
        lastTouched = jsonObject.getLong("lastTouched");
    }
    public static final Creator<Topic> CREATOR = new Creator<Topic>() {
        @Override
        public Topic createFromParcel(Parcel source) {
            return new Topic(source);
        }

        @Override
        public Topic[] newArray(int size) {
            return new Topic[size];
        }
    };

    public String toString() {
        return ("id = " + id + ";title = " + title + ";url = " + url + ";content = " + content +
                ";content_rendered = " + contentRendered + ";replies = " + replies + ";member = "
                + member + ";node = " + node + ";created = " + created + ";last_modified = " + lastModified
                + ";last_touched = " + lastTouched);
    }
}
